<?php
namespace App\Http\Controllers\v1;

use Illuminate\Http\Request;
use Mockery\Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\RequestBackController;
use App\Http\Controllers\Controller;
use App\Http\Controllers\CheckController;
use App\Http\Controllers\v1\VisaController;
/*
   审批模板
*/
class ApprovalCenterController extends Controller{
    /**
    所有案件审批approval展示(分页、模糊查询(项目名称、主体名称、客户名称==？？申请人名称))temp=>是否为我发起的我审批的我抄送的
     */
    public function ShowApproval($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['page','pageSize','temp','UserId','key'])){
            return RequestBackController::Structure(502);
        }else{
            // var_dump($pos['UserId']);
            $page = isset($pos['page']) ? $pos['page']  : 1;
            $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
            $offset = ($page - 1) * $pageSize;
            $map = array();
            if($pos['temp'] == 1){//我抄送的
                // print_r($pos["UserId"]);die();
                $ApprovalInfo  =DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_title as ApprovalModeTitle','c.case_id as CaseId','c.case_title as CaseTitle','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                    ->join("yl_user as u",function($join){
                        $join->on('a.approval_creater_id','=','u.user_id');
                    })
                    ->join("yl_case as c",function($join){
                        $join->on('c.case_id','=','a.case_id');
                    })
                    ->join("yl_approval_mode as m",function($join){
                        $join->on('a.approval_mode_id','=','m.approval_mode_id');
                    })
                    ->join("yl_approval_results as r",function($join){
                        $join->on('a.approval_results_id','=','r.approval_results_id');
                    })
                    ->whereRaw("FIND_IN_SET($pos[UserId],m.approval_copy_id)")
					->where('m.approval_mode_title','like','%'.$pos['key'].'%');

                $count =$ApprovalInfo->count();
                $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                    ->get()
                    ->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($pos['page'] > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
                    if($ApprovalInfo){
                        foreach ($ApprovalInfo as $k => $v) {
                            $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = date('Y-m-d',$v['ApprovalModeCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = date('Y-m-d',$v['ApprovalModeUpdateTime']);
                            $ApprovalInfo['count'] = $count;
                        }
                        return RequestBackController::Structure(501,$ApprovalInfo);
                    }else{
                        return RequestBackController::Structure(504);
                    }

                }
            }
            if($pos['temp'] == 2){//我审批的
                //$map['m.approval_assessor_id']=['like','%'.$pos['UserId'].'%'];
                $ApprovalInfo  =DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_title as ApprovalModeTitle','c.case_id as CaseId','c.case_title as CaseTitle','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                    ->join("yl_user as u",function($join){
                        $join->on('a.approval_creater_id','=','u.user_id');
                    })
                    ->join("yl_case as c",function($join){
                        $join->on('c.case_id','=','a.case_id');
                    })
                    ->join("yl_approval_mode as m",function($join){
                        $join->on('a.approval_mode_id','=','m.approval_mode_id');
                    })
                    ->join("yl_approval_results as r",function($join){
                        $join->on('a.approval_results_id','=','r.approval_results_id');
                    })
                    // ->where('m.approval_assessor_id','like','%'.$pos['UserId'].'%');
                    ->whereRaw("FIND_IN_SET($pos[UserId],m.approval_assessor_id)")
					->where('m.approval_mode_title','like','%'.$pos['key'].'%');
                $count =$ApprovalInfo->count();
                $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                    ->get()
                    ->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['CaseId'];
                    })
                    ->toArray();
                //var_dump($ApprovalInfo);die();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($pos['page'] > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
                    if($ApprovalInfo){
                        foreach ($ApprovalInfo as $k => $v) {
                            $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = date('Y-m-d',$v['ApprovalModeCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = date('Y-m-d',$v['ApprovalModeUpdateTime']);
                            $ApprovalInfo['count'] = $count;
                        }
                        return RequestBackController::Structure(501,$ApprovalInfo);
                    }else{
                        return RequestBackController::Structure(504);
                    }

                }

            }if($pos['temp'] == 3){//我发起的
                //$map['m.approval_mode_creater']=['like','%'.$pos['UserId'].'%'];
                $ApprovalInfo  =DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_title as ApprovalModeTitle','c.case_id as CaseId','c.case_title as CaseTitle','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                    ->join("yl_user as u",function($join){
                        $join->on('a.approval_creater_id','=','u.user_id');
                    })
                    ->join("yl_case as c",function($join){
                        $join->on('c.case_id','=','a.case_id');
                    })
                    ->join("yl_approval_mode as m",function($join){
                        $join->on('a.approval_mode_id','=','m.approval_mode_id');
                    })
                    ->join("yl_approval_results as r",function($join){
                        $join->on('a.approval_results_id','=','r.approval_results_id');
                    })
                    // ->where('m.approval_mode_creater','like','%'.$pos['UserId'].'%');
                    ->whereRaw("FIND_IN_SET($pos[UserId],m.approval_mode_creater)")
					->where('m.approval_mode_title','like','%'.$pos['key'].'%');
                $count =$ApprovalInfo->count();
                $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                    ->get()
                    ->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['CaseId'];
                    })
                    ->toArray();
                //var_dump($ApprovalInfo);die();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($pos['page'] > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
                    if($ApprovalInfo){
                        foreach ($ApprovalInfo as $k => $v) {
                            $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = date('Y-m-d',$v['ApprovalModeCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = date('Y-m-d',$v['ApprovalModeUpdateTime']);
                            $ApprovalInfo['count'] = $count;
                        }
                        return RequestBackController::Structure(501,$ApprovalInfo);
                    }else{
                        return RequestBackController::Structure(504);
                    }

                }
            }else{//显示全部
                $ApprovalInfo  =DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_title as ApprovalModeTitle','c.case_id as CaseId','c.case_title as CaseTitle','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                    ->join("yl_user as u",function($join){
                        $join->on('a.approval_creater_id','=','u.user_id');
                    })
                    ->join("yl_case as c",function($join){
                        $join->on('c.case_id','=','a.case_id');
                    })
                    ->join("yl_approval_mode as m",function($join){
                        $join->on('a.approval_mode_id','=','m.approval_mode_id');
                    })
                    ->join("yl_approval_results as r",function($join){
                        $join->on('a.approval_results_id','=','r.approval_results_id');
                    })
					->where('m.approval_mode_title','like','%'.$pos['key'].'%');
                $count =$ApprovalInfo->count();
                $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                    ->get()
                    ->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['CaseId'];
                    })
                    ->toArray();
                //var_dump($ApprovalInfo);die();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($pos['page'] > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
                    if($ApprovalInfo){
                        foreach ($ApprovalInfo as $k => $v) {
                            $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = date('Y-m-d',$v['ApprovalModeCreatedTime']);
                            $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = date('Y-m-d',$v['ApprovalModeUpdateTime']);
                            $ApprovalInfo['count'] = $count;
                        }
                        return RequestBackController::Structure(501,$ApprovalInfo);
                    }else{
                        return RequestBackController::Structure(504);
                    }

                }


            }


        }

    }

    /**
    单个案件审批approval展示(分页、模糊查询(项目名称、主体名称、客户名称==？？申请人名称))
     */
    public function CaseApproval($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['page','pageSize','CaseId'])){
            return RequestBackController::Structure(502);
        }else{
            $page = isset($pos['page']) ? $pos['page']  : 1;
            $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
            $offset = ($page - 1) * $pageSize;
            $ApprovalInfo  =DB::connection('mysql_other')->table('yl_approval as a')->select('c.case_id as CaseId','c.case_title as CaseTitle','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle','m.approval_mode_title as ApprovalModeTitle')
                ->join("yl_user as u",function($join){
                    $join->on('a.approval_creater_id','=','u.user_id');
                })
                ->join("yl_case as c",function($join){
                    $join->on('c.case_id','=','a.case_id');
                })
                ->join("yl_approval_mode as m",function($join){
                    $join->on('a.approval_mode_id','=','m.approval_mode_id');
                })
                ->join("yl_approval_results as r",function($join){
                    $join->on('a.approval_results_id','=','r.approval_results_id');
                })
                ->where('c.case_id','=',$pos['CaseId']);
            $count =$ApprovalInfo->count();
            $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                ->get()->map(function ($val){
                    $val = (array)$val;
                    return $val;
                })
                ->keyBy(function($item)
                {
                    return $item['CaseId'];
                })
                ->toArray();
            //var_dump($ApprovalInfo);die();
            $sum_page = ceil($count/$pageSize);  //总页码
            if($pos['page'] > $sum_page && $sum_page > 0 ){
                return RequestBackController::Structure(301);
            }else{
                if($ApprovalInfo){
                    foreach ($ApprovalInfo as $k => $v) {
                        $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
						$ApprovalInfo[$k]['ApprovalContent'] = json_decode($v['ApprovalContent']);
                    }
                    return RequestBackController::Structure(501,$ApprovalInfo);
                }else{
                    return RequestBackController::Structure(504);
                }

            }

        }

    }
	/*
    审批显示
    */
    public function ShowApprovalList($request){
        // print_r(1111);die();
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['page','pageSize','temp','UserId','key'])){
            return RequestBackController::Structure(502);
        }else{
            $page = isset($pos['page']) ? $pos['page']  : 1;
            $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
            $offset = ($page - 1) * $pageSize;
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){//超级管理员显示所有
                if($pos['temp'] == 1){//我抄送的
                    $ApprovalInfo = DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_title as ApprovalModeTitle','a.approval_copy_id as ApprovalCopyId','a.approval_assessor_id as ApprovalAssessorId','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_id as ApprovalId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_type_id as ApprovalTypeId','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                        ->join("yl_user as u",function($join){
                            $join->on('a.approval_creater_id','=','u.user_id');
                        })
                        ->join("yl_approval_mode as m",function($join){
                            $join->on('a.approval_mode_id','=','m.approval_mode_id');
                        })
                        ->join("yl_approval_results as r",function($join){
                            $join->on('a.approval_results_id','=','r.approval_results_id');
                        })
                        ->whereRaw("FIND_IN_SET($pos[UserId],a.approval_copy_id)")
                        ->where('m.approval_mode_title','like','%'.$pos['key'].'%');
                }
                if ($pos['temp']==2){
                    $ApprovalInfo = DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_title as ApprovalModeTitle','a.approval_copy_id as ApprovalCopyId','a.approval_assessor_id as ApprovalAssessorId','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_id as ApprovalId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_type_id as ApprovalTypeId','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                        ->join("yl_user as u",function($join){
                            $join->on('a.approval_creater_id','=','u.user_id');
                        })
                        ->join("yl_approval_mode as m",function($join){
                            $join->on('a.approval_mode_id','=','m.approval_mode_id');
                        })
                        ->join("yl_approval_results as r",function($join){
                            $join->on('a.approval_results_id','=','r.approval_results_id');
                        })
                        ->whereRaw("FIND_IN_SET($pos[UserId],a.approval_assessor_id)")
                        ->where('m.approval_mode_title','like','%'.$pos['key'].'%');
                }
                if($pos['temp'] ==3){
                    $ApprovalInfo = DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_title as ApprovalModeTitle','a.approval_copy_id as ApprovalCopyId','a.approval_assessor_id as ApprovalAssessorId','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_id as ApprovalId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_type_id as ApprovalTypeId','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                        ->join("yl_user as u",function($join){
                            $join->on('a.approval_creater_id','=','u.user_id');
                        })
                        ->join("yl_approval_mode as m",function($join){
                            $join->on('a.approval_mode_id','=','m.approval_mode_id');
                        })
                        ->join("yl_approval_results as r",function($join){
                            $join->on('a.approval_results_id','=','r.approval_results_id');
                        })
                        ->whereRaw("FIND_IN_SET($pos[UserId],m.approval_mode_creater)")
                        ->where('m.approval_mode_title','like','%'.$pos['key'].'%');
                }
                $count =$ApprovalInfo->count();
                $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)->orderBy('ApprovalId','desc')
                    ->get()
                    ->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['ApprovalId'];
                    })
                    ->toArray();
                $sum_page = ceil($count/$pageSize);  //总页码
				$user =  DB::connection('mysql_other')->table('yl_case')->select('case_id as CaseId','case_title as CaseTitle')->get()
                    ->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
					->keyBy(function($item)
                    {
                        return $item['CaseId'];
                    })
                    ->toArray();
					//print_r($user);die;
                if($pos['page'] > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
                    if($ApprovalInfo){
                        foreach ($ApprovalInfo as $k => $v) {
							$ApprovalInfo[$k]['ApprovalContent']=json_decode($v['ApprovalContent']);
							$ApprovalInfo[$k]['ApprovalModeContent']=json_decode($v['ApprovalModeContent']);
							if($v['CaseId']>0 || !empty($v['CaseId'])){
								$ApprovalInfo[$k]['CaseTitle'] = isset($user[$v['CaseId']]['CaseTitle'])?$user[$v['CaseId']]['CaseTitle']:'';
							}else{
								$ApprovalInfo[$k]['CaseTitle'] ="";
							}
                            if($v['ApprovalCreatedTime']>0 && $v['ApprovalCreatedTime'] !=null && $v['ApprovalCreatedTime'] !=""){
                                $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                            }else{
                                $ApprovalInfo[$k]['ApprovalCreatedTime'] = "";
                            }
                            if($v['ApprovalModeCreatedTime']>0 && $v['ApprovalModeCreatedTime'] !=null && $v['ApprovalModeCreatedTime'] !=""){
                                $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = date('Y-m-d',$v['ApprovalModeCreatedTime']);
                            }else{
                                $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = "";
                            }
                            if($v['ApprovalModeUpdateTime']>0 && $v['ApprovalModeUpdateTime'] !=null && $v['ApprovalModeUpdateTime'] !=""){
                                $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = date('Y-m-d',$v['ApprovalModeUpdateTime']);
                            }else{
                                $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = "";
                            }
                            
                        }

						rsort($ApprovalInfo);
                        $ApprovalsInfo['Count'] = $count;
                        $ApprovalsInfo['List'] = $ApprovalInfo;
                        return RequestBackController::Structure(501,$ApprovalsInfo);
                    }else{
                        return RequestBackController::Structure(504);
                    }

                }

            }
            if($UserPurview == 'Personal'){
                if($pos['temp'] == 1){//我抄送的
                    $ApprovalInfo = DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_title as ApprovalModeTitle','a.approval_copy_id as ApprovalCopyId','a.approval_assessor_id as ApprovalAssessorId','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_id as ApprovalId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_type_id as ApprovalTypeId','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                    ->join("yl_user as u",function($join){
                        $join->on('a.approval_creater_id','=','u.user_id');
                    })
                    ->join("yl_approval_mode as m",function($join){
                        $join->on('a.approval_mode_id','=','m.approval_mode_id');
                    })
                    ->join("yl_approval_results as r",function($join){
                        $join->on('a.approval_results_id','=','r.approval_results_id');
                    })
                    ->whereRaw("FIND_IN_SET($pos[UserId],a.approval_copy_id)")
					->where('m.approval_mode_title','like','%'.$pos['key'].'%');
                }
                if($pos['temp']==2){//我审批的
                    $ApprovalInfo = DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_title as ApprovalModeTitle','a.approval_copy_id as ApprovalCopyId','a.approval_assessor_id as ApprovalAssessorId','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_id as ApprovalId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_type_id as ApprovalTypeId','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                    ->join("yl_user as u",function($join){
                        $join->on('a.approval_creater_id','=','u.user_id');
                    })
                    ->join("yl_approval_mode as m",function($join){
                        $join->on('a.approval_mode_id','=','m.approval_mode_id');
                    })
                    ->join("yl_approval_results as r",function($join){
                        $join->on('a.approval_results_id','=','r.approval_results_id');
                    })
                    ->whereRaw("FIND_IN_SET($pos[UserId],a.approval_assessor_id)")
					->where('m.approval_mode_title','like','%'.$pos['key'].'%');
                }
                if($pos['temp'] ==3){//我发起的审批
                    $ApprovalInfo = DB::connection('mysql_other')->table('yl_approval as a')->select('m.approval_mode_title as ApprovalModeTitle','a.approval_copy_id as ApprovalCopyId','a.approval_assessor_id as ApprovalAssessorId','u.user_id as UserId','u.user_name as UserName','a.case_id as CaseId','a.approval_mode_id as ApprovalModeId','a.approval_id as ApprovalId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_assessor_id as ApprovalAssessorId','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_type_id as ApprovalTypeId','m.approval_mode_update_time as ApprovalModeUpdateTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                    ->join("yl_user as u",function($join){
                        $join->on('a.approval_creater_id','=','u.user_id');
                    })
                    ->join("yl_approval_mode as m",function($join){
                        $join->on('a.approval_mode_id','=','m.approval_mode_id');
                    })
                    ->join("yl_approval_results as r",function($join){
                        $join->on('a.approval_results_id','=','r.approval_results_id');
                    })
                    ->whereRaw("FIND_IN_SET($pos[UserId],m.approval_mode_creater)")
					->where('m.approval_mode_title','like','%'.$pos['key'].'%');

                }

                    $count =$ApprovalInfo->count();
                    $ApprovalInfo=$ApprovalInfo->offset($offset)->limit($pageSize)
                        ->get()
                        ->map(function ($val){
                            $val = (array)$val;
                            return $val;
                        })
                        ->keyBy(function($item)
                        {
                            return $item['ApprovalId'];
                        })
                        ->toArray();
                    $user =  DB::connection('mysql_other')->table('yl_case')->select('case_id as CaseId','case_title as CaseTitle')->get()
                    ->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->keyBy(function($item)
                    {
                        return $item['CaseId'];
                    })
                    ->toArray();
                    $sum_page = ceil($count/$pageSize); 
                    //总页码
                    if($pos['page'] > $sum_page && $sum_page > 0 ){
                        return RequestBackController::Structure(301);
                    }else{
                        if($ApprovalInfo){
                            foreach ($ApprovalInfo as $k => $v) {
                                $ApprovalInfo[$k]['ApprovalContent']=json_decode($v['ApprovalContent']);
                                $ApprovalInfo[$k]['ApprovalModeContent']=json_decode($v['ApprovalModeContent']);
                                if($v['CaseId']>0 || !empty($v['CaseId'])){
                                    $ApprovalInfo[$k]['CaseTitle'] = isset($user[$v['CaseId']]['CaseTitle'])?$user[$v['CaseId']]['CaseTitle']:'';
                                }else{
                                    $ApprovalInfo[$k]['CaseTitle'] ="";
                                }
                                if($v['ApprovalCreatedTime']>0 && $v['ApprovalCreatedTime'] !=null && $v['ApprovalCreatedTime'] !=""){
                                    $ApprovalInfo[$k]['ApprovalCreatedTime'] = date('Y-m-d',$v['ApprovalCreatedTime']);
                                }else{
                                    $ApprovalInfo[$k]['ApprovalCreatedTime'] = "";
                                }
                                if($v['ApprovalModeCreatedTime']>0 && $v['ApprovalModeCreatedTime'] !=null && $v['ApprovalModeCreatedTime'] !=""){
                                    $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = date('Y-m-d',$v['ApprovalModeCreatedTime']);
                                }else{
                                    $ApprovalInfo[$k]['ApprovalModeCreatedTime'] = "";
                                }
                                if($v['ApprovalModeUpdateTime']>0 && $v['ApprovalModeUpdateTime'] !=null && $v['ApprovalModeUpdateTime'] !=""){
                                    $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = date('Y-m-d',$v['ApprovalModeUpdateTime']);
                                }else{
                                    $ApprovalInfo[$k]['ApprovalModeUpdateTime'] = "";
                                }
                                
                            }
                            rsort($ApprovalInfo);
                            $ApprovalsInfo['Count'] = $count;
                            $ApprovalsInfo['List'] = $ApprovalInfo;
                            return RequestBackController::Structure(501,$ApprovalsInfo);
                        }else{
                            return RequestBackController::Structure(504);
                        }

                    }
            }
            if($UserPurview == 'None'){//暂无任何权限
                return RequestBackController::Structure(403);
            }
        }

    }
    /*
        审批分类新建
        */
    public function AddApprovalType($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalTypeTitle'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $add=array();
                $add['approval_type_title'] = $pos['ApprovalTypeTitle'];
                $row = DB::connection('mysql_other')->table('yl_approval_type')->insert($add);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }else{
                return RequestBackController::Structure(403);
            }


        }

    }
    /*编辑审批分类*/
    public function EditApprovalType($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalTypeTitle','ApprovalTypeId'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $map=array();
                $map['approval_type_title'] = $pos['ApprovalTypeTitle'];
                $row = DB::connection('mysql_other')->table('yl_approval_type')->where('approval_type_id',$pos['ApprovalTypeId'])->update($map);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }else{
                return RequestBackController::Structure(403);
            }


        }

    }
    /*删除审批分类*/
    public function DeleteApprovalType($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalTypeId'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                if($pos['ApprovalTypeId'] > 0 ){
                    // print_r($pos['ApprovalTypeId']);die();
                    $approval = DB::connection('mysql_other')->table('yl_approval_mode')->where('approval_type_id',$pos['ApprovalTypeId'])->first();
                    if($approval){
                        return RequestBackController::Structure(604);//不能删除。模板中含有该分类
                    }else{
                        $row = DB::connection('mysql_other')->table('yl_approval_type')->where('approval_type_id',$pos['ApprovalTypeId'])->delete();
                        if($row){
                            return RequestBackController::Structure(601);
                        }else{
                            return RequestBackController::Structure(602);
                        }
                    }
                }else{
                    return RequestBackController::Structure(505);
                }
            }else{
                return RequestBackController::Structure(403);
            }


        }

    }
	/*审批分类显示*/
     public function ShowApprovalType($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $page = isset($pos['page']) ? $pos['page']  : 1;
        $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
        $offset = ($page - 1) * $pageSize;
        if(!CheckController::CheckParameter($pos,['UserId'])){
            return RequestBackController::Structure(502);
        }else{
            $row = DB::connection('mysql_other')->table('yl_approval_type')->select('approval_type_id as ApprovalTypeId','approval_type_title as ApprovalTypeTitle','approval_type_created_time as ApprovalTypeCreatedTime');
                $count =$row->count();
                $row=$row->offset($offset)->limit($pageSize)
                ->get()->map(function ($val){
                        $val = (array)$val;
                        return $val;
                    })
                    ->toArray();
                $sum_page = ceil($count/$pageSize);  //总页码
                if($page > $sum_page && $sum_page > 0 ){
                    return RequestBackController::Structure(301);
                }else{
					//print_r($row);die;
                    if(isset($row)){
						foreach ($row as $k => $v) {
                           $row[$k]['ApprovalTypeCreatedTime']=isset($v['ApprovalTypeCreatedTime'])?date('Y-m-d',$v['ApprovalTypeCreatedTime']):'';
                        }
						$rows['List'] =$row;
						$rows['Count'] =$count;
                        return RequestBackController::Structure(501,$rows);
                    }else{
                        return RequestBackController::Structure(504);
                    }
                           
                }
                
        }

    }

   
	 /*
    操作审批
    */
    public function OperateApproval($request){
            if(!CheckController::CheckUser($request)){
                return RequestBackController::Structure(205);
            }
            $pos=$request->all();
            if(!CheckController::CheckParameter($pos,['UserId','ApprovalResultsId','ApprovalId'])){
                return RequestBackController::Structure(502);
            }else{
                //判断是否有权限操作审批
                $RowIn =DB::connection('mysql_other')->table('yl_approval_mode')->where('approval_assessor_id',$pos['UserId'])
                                                                                ->first(); 
                if($RowIn){//有权限审批
                    //添加审批记录进表
                    $add = array();
                    $add['approval_id'] = $pos['ApprovalId'];
                    $add['approval_assessor_id'] = $pos['UserId'];
                    $add['approval_results_id'] =$pos['ApprovalResultsId'];
                    $add['approval_deal_time'] =time();
                    $addDispose = DB::connection('mysql_other')->table('yl_approval_dispose')->insert($add);
                    if($addDispose){//处理成功将数据存入审批表
						$VisaController=new VisaController();
						$a_temp=$VisaController->ApprovalVisa($pos['ApprovalId']);
                        $up=array();
						
                        $up['approval_results_id'] = $pos['ApprovalResultsId'];
                        $addDispose = DB::table('yl_approval')->where('approval_id',$pos['ApprovalId'])->update($up);
                        if($addDispose){//处理成功
                            $findResults = DB::connection('mysql_other')->table('yl_approval')->where('approval_id',$pos['ApprovalId'])->first();
                            if($findResults->approval_results_id==1){//审批通过。查找下一个
                                $assessor = $findResults->approval_assessors_id;
                                $foo = explode(',',$assessor);
                                $_temp = array_flip($foo);//键值键名交换位置
                                if(isset($foo[$_temp[$pos['UserId']]+1])){//查询下一个人是否存在将审批表数据当前审批人修改为当前查询的人
                                    $map = array();
                                    $map['approval_assessor_id'] =  $foo[$_temp[$pos['UserId']]+1];
                                    $upRow = DB::connection('mysql_other')->table('yl_approval')->where('approval_id',$pos['ApprovalId'])->update($map);
                                    if($upRow){
                                        return RequestBackController::Structure(601);
                                    }else{
                                        return RequestBackController::Structure(602);
                                    }
                                }
                            }
                            if($findResults->approval_results_id==2){//审批终止
                                return RequestBackController::Structure(601);
                            }
                            return RequestBackController::Structure(601);
                        }
						else
						{
							return RequestBackController::Structure(603);
						}
                    }else{
                        return RequestBackController::Structure(602);
                    }
                }else{
					return RequestBackController::Structure(403);
				}
            }
    }

   /*
       显示审批模板
   */
    public function ShowApprovalMode($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        $page = isset($pos['page']) ? $pos['page']  : 1;
        $pageSize = isset($pos['pageSize']) ? $pos['pageSize'] : 10;
        $offset = ($page - 1) * $pageSize;
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalTypeId'])){
            return RequestBackController::Structure(502);
        }else{
            $row = DB::connection('mysql_other')->table('yl_approval_mode as m')->select('m.approval_assessor_id as ApprovalAssessorId','m.approval_mode_id as ApprovalModeId','m.approval_mode_content as ApprovalModeContent','m.approval_mode_title as ApprovalModeTitle','m.approval_copy_id as ApprovalCopyId','m.approval_mode_creater as ApprovalModeCreater','m.approval_mode_created_time as ApprovalModeCreatedTime','m.approval_mode_updater as ApprovalModeUpdater','m.approval_mode_update_time as ApprovalModeUpdateTime','m.approval_type_id as ApprovalTypeId','t.approval_type_id as ApprovalTypeId','t.approval_type_title as ApprovalTypeTitle','u.user_id as UserId','u.user_name as UserName')
                ->join("yl_approval_type as t",function($join){
                    $join->on('m.approval_type_id','=','t.approval_type_id');
                })->join("yl_user as u",function($join){
                    $join->on('u.user_id','=','m.approval_mode_creater');
                })
                ->where('m.approval_type_id',$pos['ApprovalTypeId']);
            $count =$row->count();
            $row=$row->offset($offset)->limit($pageSize)
                ->get()->map(function ($val){
                    $val = (array)$val;
                    return $val;
                })
                ->toArray();
            $sum_page = ceil($count/$pageSize);  //总页码
            if($page > $sum_page && $sum_page > 0 ){
                return RequestBackController::Structure(301);
            }else{
                if($row){
                    foreach ($row as $k => $v) {
                        if($v['ApprovalModeCreatedTime']>0 && $v['ApprovalModeCreatedTime'] !=null && $v['ApprovalModeCreatedTime'] !="") {
                            $row[$k]['ApprovalModeCreatedTime'] =date('Y-m-d',$v['ApprovalModeCreatedTime']);
                        }else{
                            $row[$k]['ApprovalModeCreatedTime'] ="";
                        }
                        if($v['ApprovalModeUpdateTime']>0 && $v['ApprovalModeUpdateTime'] !=null && $v['ApprovalModeUpdateTime'] !="") {
                            $row[$k]['ApprovalModeUpdateTime'] =date('Y-m-d',$v['ApprovalModeUpdateTime']);
                        }else{
                            $row[$k]['ApprovalModeUpdateTime'] ="";
                        }
                        if($v['ApprovalModeContent']){
                            $row[$k]['ApprovalModeContent'] = json_decode($v['ApprovalModeContent']);
                        }
                        
                    }
					$rows['Count']=$count;
                    $rows['List']=$row;
                    return RequestBackController::Structure(501,$rows);
                }else{
                    return RequestBackController::Structure(504);
                }
            }
        }
    }
    /*
       新建审批模板
   */
    public function AddApprovalMode($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalModeContent','ApprovalAssessorId','ApprovalCopyId','ApprovalTypeId','ApprovalModeTitle'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $add=array();
                $add['approval_mode_title'] = $pos['ApprovalModeTitle'];
                $add['approval_mode_content'] = json_encode($pos['ApprovalModeContent']);//模板内容
                $add['approval_assessor_id'] = $pos['ApprovalAssessorId'];//审批人合集
                $add['approval_copy_id'] = $pos['ApprovalCopyId'];//抄送人合集
                $add['approval_mode_creater'] = $pos['UserId'];
                $add['approval_mode_created_time'] = time();
                $add['approval_type_id'] = $pos['ApprovalTypeId'];//审批类型
                $add['approval_mode_updater'] = $pos['UserId'];
                $add['approval_mode_update_time'] = time();
                $row = DB::connection('mysql_other')->table('yl_approval_mode')->insert($add);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }else{
                return RequestBackController::Structure(403);
            }


        }

    }


    /*
    编辑审批模板
    */
    public function EditApprovalMode($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalModeId','ApprovalModeTitle','ApprovalModeContent','ApprovalAssessorId','ApprovalCopyId','ApprovalTypeId'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $map=array();
                $map['approval_mode_title'] = $pos['ApprovalModeTitle'];
                $map['approval_mode_content'] = json_encode($pos['ApprovalModeContent']);//模板内容
                $map['approval_assessor_id'] = $pos['ApprovalAssessorId'];//审批人合集
                $map['approval_copy_id'] = $pos['ApprovalCopyId'];//抄送人合集
                $map['approval_type_id'] = $pos['ApprovalTypeId'];//审批类型
                $map['approval_mode_updater'] = $pos['UserId'];
                $map['approval_mode_update_time'] = time();
                $row = DB::connection('mysql_other')->table('yl_approval_mode')->where('approval_mode_id',$pos['ApprovalModeId'])->update($map);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }

            }else{
                return RequestBackController::Structure(403);
            }


        }

    }

   
     /*
       删除审批模板
   */
    public function DeleteApprovalMode($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalModeId'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){//管理员删除
                $rowIn = DB::connection('mysql_other')->table('yl_approval_mode')->where('approval_mode_id',$pos['ApprovalModeId'])->first();
                if($rowIn){
                    $row = DB::connection('mysql_other')->table('yl_approval_mode')->where('approval_mode_id',$pos['ApprovalModeId'])->delete();
                    if($row){
                        return RequestBackController::Structure(601);
                    }else{
                        return RequestBackController::Structure(602);
                    }
                }else{
                    return RequestBackController::Structure(505);
                }
            }
            else{
                return RequestBackController::Structure(403);
            }


        }

    }
     /*
    新建审批（登录可建）
    */
    public function AddApproval($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalModeId','CaseId','ApprovalContent'])){
            return RequestBackController::Structure(502);
        }else{
            $add=array();
            $RowMode = DB::connection('mysql_other')->table('yl_approval_mode')->where('approval_mode_id',$pos['ApprovalModeId'])->first();
			//print_r($RowMode);die;
            if(isset($RowMode)){//存在该模板数据
                $assessor = $RowMode->approval_assessor_id;
                $foo = explode(',',$assessor);
				//print_r($foo[0]);die;
                $add['approval_copy_id'] = $RowMode->approval_copy_id;//将抄送者存放进审批中表可查看功能
                $add['approval_assessor_id'] = $foo[0];//将当前首个审批者添加
				if(isset($pos['CaseExecutorId']))
				{
					if(!empty($pos['CaseExecutorId']))
						$foo=$pos['CaseExecutorId'].','.$foo;
				}
                
				$add['approval_mode_id'] = $pos['ApprovalModeId'];//模板ID
            }else{
				return RequestBackController::Structure(505);
			}
            
            $add['case_id'] = $pos['CaseId'];//案件id
            $add['approval_content'] = json_encode($pos['ApprovalContent']);//审批主体
            $add['approval_creater_id'] = $pos['UserId'];//审批发起人
            $add['approval_created_time'] = time();//发起时间
            $rowId = DB::connection('mysql_other')->table('yl_approval')->insertGetId($add);
            if($rowId){
                return RequestBackController::Structure(601,$rowId);
            }else{
                return RequestBackController::Structure(602);
            }

        }
    }

    /*
    删除审批（仅自己可删）
    */
    public function DeleteApproval($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalId'])){
            return RequestBackController::Structure(502);
        }else{
            $rowApp = DB::connection('mysql_other')->table('yl_approval')->where('approval_id',$pos['ApprovalId'])->first();
            if($rowApp){//查询当前创建的审批是否是该用户
                if($pos['UserId'] == $rowApp->approval_creater_id){
                    $row = DB::connection('mysql_other')->table('yl_approval')->where('approval_id',$pos['ApprovalId'])->delete();
                    if($row){
                        return RequestBackController::Structure(601);
                    }else{
                        return RequestBackController::Structure(602);
                    }
                }else{
                    //暂无权限删除
                    return RequestBackController::Structure(403);
                }
            }
        }

    }

    /*
    显示审批处理结果
    */
    public function ShowApprovalResults($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId'])){
            return RequestBackController::Structure(502);
        }else{
            $row = DB::connection('mysql_other')->table('yl_approval_results')->select('approval_results_id as ApprovalResultsId','approval_results_title as ApprovalResultsTitle')
                ->get()->map(function ($val){
                    $val = (array)$val;
                    return $val;
                })
                ->toArray();
            if($row){
                return RequestBackController::Structure(501,$row);
            }else{
                return RequestBackController::Structure(504);
            }

        }

    }
    /*
       审批详情（将审批内容和留言）
       */
    public function ApprovalDetails($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['CaseId','ApprovalId','ApprovalContent','ApprovalMessage','ApprovalResultsId'])){
            return RequestBackController::Structure(502);
        }else{
            /*$Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            if($UserPurview == 'All'){
                $map = array();
                $map['approval_content'] =$pos['ApprovalContent'];
                $map['approval_message'] =$pos['ApprovalMessage'];
                $map['approval_results_id'] =$pos['ApprovalResultsId'];
                $UpApprovalContent = DB::connection('mysql_other')->table('yl_approval')->where('approval_id',$pos['ApprovalId'])->update($map);
                if($UpApprovalContent){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }*/

            $map = array();
            $map['approval_content'] =$pos['ApprovalContent'];
            $map['approval_message'] =$pos['ApprovalMessage'];
            $map['approval_results_id'] =$pos['ApprovalResultsId'];
            $UpApprovalContent = DB::connection('mysql_other')->table('yl_approval')->where('approval_id',$pos['ApprovalId'])->update($map);
            if($UpApprovalContent){
                return RequestBackController::Structure(601);
            }else{
                return RequestBackController::Structure(602);
            }
        }

    }
	/*
       查看审批详情
      */
    public function UserApproval($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalId'])){
            return RequestBackController::Structure(502);
        }else{
            $row = DB::connection('mysql_other')->table('yl_approval')->where('approval_id',$pos['ApprovalId'])->first();
            if($row){
                if($row->approval_creater_id == $pos['UserId']){
                    $arr['IsSelf'] ='true';//是否是自己点开的详情
                }else{
                    $arr['IsSelf'] ='false';
                }
				//print_r($arr);die;
                $UserApprovalInfo = DB::connection('mysql_other')->table('yl_approval as a')->select('a.approval_id as ApprovalId','a.approval_mode_id as ApprovalModeId','a.case_id as CaseId','a.approval_content as ApprovalContent','a.approval_creater_id as ApprovalCreaterId','a.approval_created_time as ApprovalCreatedTime','a.approval_results_id as ApprovalResultsId','a.approval_assessor_id as ApprovalAssessorId','a.approval_copy_id as ApprovalCopyId','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
                                        ->join("yl_approval_results as r",function($join){
                                            $join->on('a.approval_results_id','r.approval_results_id');
                                        })
                                        ->where('approval_id',$pos['ApprovalId'])
                                                ->first();
                $message=DB::connection('mysql_other')->table('yl_approval_message as m')->select('m.approval_message_id as ApprovalMessageId','m.approval_id as ApprovalId','m.message_content as MessageContent','m.message_creater_id as MessageCreaterId','m.message_created_time as MessageCreatedTime')->where('m.approval_id',$pos['ApprovalId'])
                                                ->get()->map(function ($val){
                                                    $val = (array)$val;
                                                    return $val;
                                                })
                                                ->toArray();
                $dispose = DB::connection('mysql_other')->table('yl_approval_dispose as d')->select('d.approval_dispose_id as ApprovalDisposeId','d.approval_id as ApprovalId','d.approval_assessor_id as ApprovalAssessorId','d.approval_results_id as ApprovalResultsId','d.approval_deal_time as ApprovalDealTime','r.approval_results_id as ApprovalResultsId','r.approval_results_title as ApprovalResultsTitle')
												->join("yl_approval_results as r",function($join){
														$join->on('d.approval_results_id','r.approval_results_id');
												})
                                                ->where('d.approval_id',$pos['ApprovalId'])
                                                ->get()->map(function ($val){
                                                    $val = (array)$val;
                                                    return $val;
                                                })
                                                ->toArray();
				$UserApprovalInfo->ApprovalCreatedTime = date('Y-m-d',$UserApprovalInfo->ApprovalCreatedTime);						
				$arr['List'] = $UserApprovalInfo;
				$arr['message'] = $message;
				$arr['dispose'] = $dispose;
                return RequestBackController::Structure(501,$arr);
            }else{
                return RequestBackController::Structure(504);
            }
           

        }

    }
	
	/*
    当前审批评论显示（权限：超级管理员、当前发起审批人、当前模板审批人)
    */
    public function  ApprovalMessage($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalId'])){
            return RequestBackController::Structure(502);
        }else{//超级管理员和自己和审批人能查看该条记录的评论
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            //是否为当前人发起的审批
            $approvalRow = DB::connection('mysql_other')->table('yl_approval')->where('approval_id',$pos['ApprovalId'])->first();
            $model = $approvalRow->approval_mode_id;//是否为审批人
            $modelRow = DB::connection('mysql_other')->table('yl_approval')
                        ->where('approval_mode_id',$model)
                        ->whereRaw("FIND_IN_SET($pos[UserId],approval_assessor_id)")
                        ->first();
            if($UserPurview == 'All' || $pos['UserId'] == $approvalRow->approval_creater_id || isset($modelRow)){
                $MessageInfo = DB::connection('mysql_other')->table('yl_approval_message as m')->select('m.approval_message_id as ApprovalMessageId','m.approval_id as ApprovalId','m.message_content as MessageContent','m.message_creater_id as MessageCreaterId','m.message_created_time as MessageCreatedTime','m.message_status as MessageStatus','u.user_id as UserId','u.user_name as UserName','u.user_img as UserImg')
                                    ->join("yl_user as u",function($join){
                                                $join->on('u.user_id','=','m.message_creater_id');
                                            })
                                    ->orderBy('m.message_created_time','desc')
                                    ->where('approval_id',$pos['ApprovalId'])
                                    ->get()->map(function ($val){
                                        $val = (array)$val;
                                        return $val;
                                        })
                                        ->keyBy(function($item)
                                        {
                                            return $item['ApprovalMessageId'];
                                        })
                                    ->toArray();

                if($MessageInfo){
                    foreach ($MessageInfo as $k => $v) {
                        $MessageInfo[$k]['MessageCreatedTime'] =date('H:s:i',$v['MessageCreatedTime']);
                        $MessageInfo[$k]['MessageCreated'] =date('Y-m-d',$v['MessageCreatedTime']);
                    }
                    //将数组进行分组再组合显示
                    $MegInfo= array(); 
                    foreach ($MessageInfo as $k1 => $v1){ 
                        $MegInfo[$v1['MessageCreated']][] = $v1; 
                    }
                    return RequestBackController::Structure(501,$MessageInfo);
                }else{
                    return RequestBackController::Structure(504);
                }
            }else{//暂无权限
                return RequestBackController::Structure(403);
            }
           

        }
    }

    /*
    当前审批添加评论(权限：超级管理员、当前发起审批人、当前模板审批人)
    */
    public function  AddApprovalMessage($request){
        if(!CheckController::CheckUser($request)){
            return RequestBackController::Structure(205);
        }
        $pos=$request->all();
        if(!CheckController::CheckParameter($pos,['UserId','ApprovalId','MessageContent'])){
            return RequestBackController::Structure(502);
        }else{
            $Purview = new PurviewController();
            $UserPurview = $Purview->PurviewVerdict($request,$pos['UserId']);
            //是否为当前人发起的审批
            $approvalRow = DB::connection('mysql_other')->table('yl_approval')->where('approval_id',$pos['ApprovalId'])->first();
            $model = $approvalRow->approval_mode_id;//是否为审批人
            $modelRow = DB::connection('mysql_other')->table('yl_approval_mode')
                        ->where('approval_mode_id',$model)
                        ->whereRaw("FIND_IN_SET($pos[UserId],approval_assessor_id)")
                        ->first();
					//print_r($modelRow);die();
            if($UserPurview == 'All' || $pos['UserId'] == $approvalRow->approval_creater_id || !empty($modelRow)){
                $add =array();
                $add['approval_id'] =$pos['ApprovalId'];
                $add['message_content'] =$pos['MessageContent'];
                $add['message_creater_id'] =$pos['UserId'];
                $add['message_created_time'] =time();
                $row = DB::connection('mysql_other')->table('yl_approval_message')->insert($add);
                if($row){
                    return RequestBackController::Structure(601);
                }else{
                    return RequestBackController::Structure(602);
                }
            }else{
                return RequestBackController::Structure(403);
            }
        }
    }

	

	





}